package com.hmall.user.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    private final RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public RedisUtil(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    // 设置缓存数据
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    // 设置带有过期时间的缓存数据
    public void setEx(String key, Object value, long timeout, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    // 获取缓存数据
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 删除缓存数据
    public Boolean delete(String key) {
        return redisTemplate.delete(key);
    }

    // 判断缓存是否存在
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    // 设置过期时间
    public Boolean expire(String key, long timeout, TimeUnit timeUnit) {
        return redisTemplate.expire(key, timeout, timeUnit);
    }

    // 获取剩余存活时间
    public Long getExpire(String key) {
        return redisTemplate.getExpire(key);
    }

    // 自增操作
    public Long increment(String key, long delta) {
        return redisTemplate.opsForValue().increment(key, delta);
    }

    // 自减操作
    public Long decrement(String key, long delta) {
        return redisTemplate.opsForValue().decrement(key, delta);
    }

    // 添加到集合
    public Boolean addToSet(String key, Object value) {
        return redisTemplate.opsForSet().add(key, value) == 1;
    }

    // 从集合中移除
    public Long removeFromSet(String key, Object... values) {
        return redisTemplate.opsForSet().remove(key, values);
    }

    // 获取集合大小
    public Long setSize(String key) {
        return redisTemplate.opsForSet().size(key);
    }

    // 添加到列表
    public Long addToList(String key, Object value) {
        return redisTemplate.opsForList().rightPush(key, value);
    }

    // 从列表中移除
    public Long removeFromList(String key, long count, Object value) {
        return redisTemplate.opsForList().remove(key, count, value);
    }

    // 获取列表长度
    public Long listSize(String key) {
        return redisTemplate.opsForList().size(key);
    }

    // 添加到哈希表
    public void addToHash(String key, String hashKey, Object value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    // 从哈希表中获取值
    public Object getFromHash(String key, String hashKey) {
        return redisTemplate.opsForHash().get(key, hashKey);
    }

    // 删除哈希表中的字段
    public Long deleteFromHash(String key, Object... hashKeys) {
        return redisTemplate.opsForHash().delete(key, hashKeys);
    }

    // 获取哈希表中的所有键值对
    public Map<Object, Object> getAllFromHash(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    // 添加到有序集合
    public Boolean addToSortedSet(String key, Object value, double score) {
        return redisTemplate.opsForZSet().add(key, value, score);
    }

    // 从有序集合中移除
    public Long removeFromSortedSet(String key, Object... values) {
        return redisTemplate.opsForZSet().remove(key, values);
    }

    // 获取有序集合的排名
    public Double rankInSortedSet(String key, Object value) {
        return redisTemplate.opsForZSet().score(key, value);
    }

    // 获取有序集合中的元素数量
    public Long sortedSetSize(String key) {
        return redisTemplate.opsForZSet().zCard(key);
    }

    // 获取指定范围内的有序集合成员
    public Set<Object> rangeInSortedSet(String key, long start, long end) {
        return redisTemplate.opsForZSet().range(key, start, end);
    }

    // 获取指定分数范围内的有序集合成员
    public Set<Object> rangeByScoreInSortedSet(String key, double minScore, double maxScore) {
        return redisTemplate.opsForZSet().rangeByScore(key, minScore, maxScore);
    }

    // 清空所有缓存
    public void flushAll() {
        redisTemplate.getConnectionFactory().getConnection().flushAll();
    }

    // 清空当前数据库的所有键
    public void flushDb() {
        redisTemplate.getConnectionFactory().getConnection().flushDb();
    }
}